// Values are separated by spaces, commas, semicolons or new lines; the separators are kept as typed. var _h2dToken = /[^\s,;]+/g; function _h2dParse(token) { var t = token.replace(/_/g, ''); var negative = false; if (t.charAt(0) === '-' || t.charAt(0) === '+') { negative = t.charAt(0) === '-'; t = t.slice(1); } // 0x1F, #1F, $1F, &H1F (VB), 1Fh (assembly), U+1F t = t.replace(/^(0x|#|\$|&h|u\+)/i, ''); if (/^[0-9a-f.]+h$/i.test(t)) t = t.slice(0, -1); var m = /^([0-9a-f]*)(?:\.([0-9a-f]*))?$/i.exec(t); if (!m || !(m[1] + (m[2] || '')).length) return null; return { negative: negative, whole: m[1] || '0', fraction: (m[2] || '').replace(/0+$/, ''), digits: m[1].length }; } // A fraction of k hex digits is f / 16^k = f * 5^(4k) / 10^(4k), so it always ends. function _h2dFraction(hex) { if (!hex) return ''; var k = hex.length * 4; var digits = (BigInt('0x' + hex) * (5n ** BigInt(k))).toString().padStart(k, '0'); return '.' + digits.replace(/0+$/, ''); } function _h2dGroup(s) { return s.replace(/\B(?=(\d{4})+(?!\d))/g, ' '); } function _h2dDecimal(p, signed) { var v = BigInt('0x' + p.whole); var width = p.digits * 4; var twos = signed && !p.negative && !p.fraction && width > 0 && v >= (1n << BigInt(width - 1)); if (twos) v -= (1n << BigInt(width)); var sign = (p.negative && (v !== 0n || p.fraction)) ? '-' : ''; return { text: sign + v.toString() + _h2dFraction(p.fraction), value: p.negative ? -v : v, twos: twos, width: width }; } function _h2dShow(id, visible) { var el = document.getElementById(id); if (el) el.style.display = visible ? '' : 'none'; } function convert(input) { var status = document.getElementById('h2d-status'); var signedEl = document.getElementById('h2d-signed'); var signed = !!(signedEl && signedEl.value === 'signed'); var good = [], bad = [], pairs = 0, printable = 0; var out = (input || '').replace(_h2dToken, function (token) { var p = _h2dParse(token); if (!p) { bad.push(token); return '?'; } var d = _h2dDecimal(p, signed); good.push({ p: p, d: d }); if (/^[0-9a-f]{2}$/i.test(token)) { pairs++; var b = parseInt(token, 16); if (b >= 0x20 && b < 0x7f || b === 10 || b === 13) printable++; } return d.text; }); // Space-separated byte pairs that are mostly printable ASCII are probably text. var count = good.length + bad.length; _h2dShow('h2d-text', count >= 4 && pairs === count && printable >= count * 0.8); if (status) { var msg = ''; if (good.length === 1 && !bad.length && !good[0].p.fraction) { var g = good[0], abs = g.d.value < 0n ? -g.d.value : g.d.value; var bits = g.d.twos ? (BigInt('0x' + g.p.whole)).toString(2).padStart(g.d.width, '0') : abs.toString(2); if (g.d.twos) msg = 'Binary ' + _h2dGroup(bits) + ', read as a signed ' + g.d.width + '-bit value.'; else msg = 'Binary ' + (g.d.value < 0n ? '-' : '') + _h2dGroup(bits) + ', octal ' + (g.d.value < 0n ? '-' : '') + abs.toString(8) + '.'; if (!signed && !g.p.negative && [8, 16, 32, 64].indexOf(g.d.width) >= 0 && g.d.value >= (1n << BigInt(g.d.width - 1))) msg += ' As a signed ' + g.d.width + '-bit value: ' + (g.d.value - (1n << BigInt(g.d.width))).toString() + '.'; } else if (good.length > 1) msg = 'Converted ' + good.length + ' values.'; if (bad.length) { var shown = bad.slice(0, 3).map(function (t) { return t.length > 12 ? t.slice(0, 12) + '...' : t; }).join(', '); msg += (msg ? ' ' : '') + (bad.length === 1 ? 'This is' : bad.length + ' values are') + ' not hex (shown as ?): ' + shown + (bad.length > 3 ? ', ...' : '') + '.'; } status.textContent = msg; } return out; } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }